unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ComCtrls, DB, DBTables, Grids, DBGrids, StdCtrls,
  ExtCtrls, DBCtrls, Buttons,Printers,ShellAPI;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    SearchEdit: TEdit;
    StatusBar1: TStatusBar;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    N10: TMenuItem;
    GridWords: TDBGrid;
    RussiaDictionaryChangeButton: TBitBtn;
    EnglishDictionaryChangeButton: TBitBtn;
    AddWordButton: TButton;
    EditButton: TButton;
    SearchLabel: TLabel;
    PopupMenu1: TPopupMenu;
    N11: TMenuItem;
    DBNavigator1: TDBNavigator;
    ConnectButton: TButton;
    clo1: TMenuItem;
    N12: TMenuItem;
    N13: TMenuItem;
    dlgPnt1: TPrintDialog;

    procedure RussiaDictionaryChangeButtonClick(Sender: TObject);
    procedure EnglishDictionaryChangeButtonClick(Sender: TObject);
    procedure AddWordButtonClick(Sender: TObject);
    procedure SearchEditChange(Sender: TObject);
    procedure EditButtonClick(Sender: TObject);
    procedure N11Click(Sender: TObject);
    procedure ConnectButtonClick(Sender: TObject);
    procedure N10Click(Sender: TObject);
    procedure clo1Click(Sender: TObject);
    procedure N13Click(Sender: TObject);
    procedure N9Click(Sender: TObject);
  private
    { Private declarations }
    function GetRussiaTranslate(id:longint):string;
    function GetEnglishTranslate(id:longint):string;

  public
    { Public declarations }
    function Translate(id1:longint;isEng:Boolean):string;
  end;

var
  MainForm: TMainForm;
  isEnglish: Boolean;
 { CurrentTable: TTable;}
implementation

uses DataModuleUnit, AddFormUnit, SQLCreator, EnglishCreator,
  RussiaCreator, EditFormUnit, Unit2;

{$R *.dfm}
   function TMainForm.GetRussiaTranslate(id:longint):string;
   begin
   end;
    function TMainForm.GetEnglishTranslate(id:longint):string;
    begin
    end;
function TMainForm.Translate(id1:longint;isEng:boolean):string;
begin

end;
procedure TMainForm.RussiaDictionaryChangeButtonClick(Sender: TObject);
begin
GridWords.DataSource := DataModule1.RussiaTableDataSource;
DBNavigator1.DataSource :=DataModule1.RussiaTableDataSource;
GridWords.Columns.Items[1].Width := 140;
isEnglish := false;
GridWords.PopupMenu := PopupMenu1;

end;

procedure TMainForm.EnglishDictionaryChangeButtonClick(Sender: TObject);
begin
DBNavigator1.DataSource :=DataModule1.EnglishTableDataSource;
GridWords.DataSource := DataModule1.EnglishTableDataSource;
GridWords.Columns.Items[1].Width := 140;
isEnglish := true;
GridWords.PopupMenu := PopupMenu1;
end;

procedure TMainForm.AddWordButtonClick(Sender: TObject);
var
addForm: TForm2;
begin
Application.CreateForm(TForm2,addForm);
addForm.SetLanguage(isEnglish);
addForm.ShowModal;
addForm.Destroy;
end;

procedure TMainForm.SearchEditChange(Sender: TObject);
begin

if(SearchEdit.Text <> '') then
begin
if(isEnglish = true)
then
begin
with DataModule1.SearchQuery do
begin
    SQL.Clear;
    SQL.Append('select *');
    SQL.Append('from EnglishDictionary');
    SQL.Append('Where englishword Like '+#34+'%'+SearchEdit.Text+'%'+#34);
     end;
     DataModule1.SearchQuery.Open;
     GridWords.DataSource := DataModule1.SearchSource;
    {when english}
    
end
else
begin
with DataModule1.SearchQuery do
begin
    SQL.Clear;
    SQL.Append('select *');
    SQL.Append('from RussiaDictionary');
    SQL.Append('Where russiaworld Like '+#34+'%'+SearchEdit.Text+'%'+#34);
     end;
      DataModule1.SearchQuery.Open;
       GridWords.DataSource := DataModule1.SearchSource;
      {when ukr}
end;
    end
    else
    begin
    if(isEnglish) then
    EnglishDictionaryChangeButtonClick(MainForm.EnglishDictionaryChangeButton)
    else
     RussiaDictionaryChangeButtonClick(MainForm.RussiaDictionaryChangeButton);

    end;
end;

procedure TMainForm.EditButtonClick(Sender: TObject);
var
strategy:ISQLCreator;
begin
Application.CreateForm(TEditForm,EditForm);
if(isEnglish) then
strategy := EnglishSQLCreator.Create
else
strategy := RussiaSQLCreator.Create;
EditForm.setStrategy(strategy);
EditForm.ShowModal;
strategy._Release;
EditForm.Destroy;
end;


procedure TMainForm.N11Click(Sender: TObject);
var
index:integer;
begin
{select RussiaDictionary.RussiaWorld
from RussiaDictionary,TranslateDictionary,EnglishDictionary
where TranslateDictionary.EnglishId = 1 AND
TranslateDictionary.EnglishId = EnglishDictionary.Id AND
TranslateDictionary.RussiaId = RussiaDictionary.Id}
   index :=  GridWords.Fields[0].AsInteger;
 if(isEnglish) then
 begin


 with DataModule1.TranslateQuery.SQL do
 begin
 Clear;
 Append('select RussiaDictionary.RussiaWorld');
 Append('from RussiaDictionary,TranslateDictionary,EnglishDictionary');
 Append('where TranslateDictionary.EnglishId ='+IntToStr(index)+' AND');
 Append('TranslateDictionary.EnglishId = EnglishDictionary.Id AND');
 Append('TranslateDictionary.RussiaId = RussiaDictionary.Id');

  end;
 end
 else
 begin

   with DataModule1.TranslateQuery.SQL do
 begin
 Clear;
 {select EnglishDictionary.EnglishWord
from RussiaDictionary,TranslateDictionary,EnglishDictionary
where TranslateDictionary.RussiaId = 1 AND
TranslateDictionary.RussiaId = RussiaDictionary.Id AND
TranslateDictionary.EnglishId = EnglishDictionary.Id}
 Append('select EnglishDictionary.EnglishWord');
 Append('from RussiaDictionary,TranslateDictionary,EnglishDictionary');
 Append('where TranslateDictionary.RussiaId ='+IntToStr(index)+' AND');
 Append('TranslateDictionary.RussiaId = RussiaDictionary.Id AND');
 Append('TranslateDictionary.EnglishId = EnglishDictionary.Id');

  end;

end;
    DataModule1.TranslateQuery.Open;
  GridWords.DataSource := DataModule1.TranslateSource;
  GridWords.PopupMenu := nil;
end;

procedure TMainForm.ConnectButtonClick(Sender: TObject);
 var AliasParams : TStringList;
      AliasDirectory : string;
      AliasName,sNewAlias,sDataDir: string;
 begin
  sNewAlias := 'dictionary1';
 sDataDir := 'db';
 Session.AddStandardAlias(sNewAlias, sDataDir, 'PARADOX') ;
 DataModule1.TranslateDictionary.DatabaseName :=  sNewAlias;
 DataModule1.EnglishDictionary.DatabaseName :=  sNewAlias;
DataModule1.RussiaDictionary.DatabaseName :=  sNewAlias;
  DataModule1.TranslateDictionary.Active := true;
   DataModule1.EnglishDictionary.Active := true;
   DataModule1.RussiaDictionary.Active := true;

end;

procedure TMainForm.N10Click(Sender: TObject);
begin
AboutBox.Showmodal;
end;

procedure TMainForm.clo1Click(Sender: TObject);
begin
close;
end;

procedure TMainForm.N13Click(Sender: TObject);

 var
 F:TextFile;
begin
if dlgPnt1.Execute then
begin
AssignPrn(F);
Rewrite(F);
//Writeln(F,(ActiveMDIChild as TChildForm).RichEdit1.Text);
System.CloseFile(F);
end;
 end;

procedure TMainForm.N9Click(Sender: TObject);
begin
ShellExecute(Application.Handle,'open','Dictionary.chm',nil,nil,SW_SHOW);
end;

end.
